package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.IOUtil;
import com.ruoyi.system.domain.OblistDetail;
import com.ruoyi.system.domain.StockLog;
import com.ruoyi.system.mapper.OblistDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OutboundListMapper;
import com.ruoyi.system.domain.OutboundList;
import com.ruoyi.system.service.IOutboundListService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 出库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-02
 */
@Service
public class OutboundListServiceImpl implements IOutboundListService 
{
    @Autowired
    private OutboundListMapper outboundListMapper;
    @Autowired
    private OblistDetailMapper oblistDetailMapper;
    @Autowired
    StockLogServiceImpl stockLogServiceImpl;

    /**
     * 处理修改出库单商品信息
     *
     * @param listid
     * @param goodsList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateOrderGoods(long listid, List<OblistDetail> goodsList) {
        //获取原来出库单商品信息
        OblistDetail detail_select = new OblistDetail();
        detail_select.setListid(listid);
        List<OblistDetail> oblistDetails = oblistDetailMapper.selectOblistDetailList(detail_select);
        //遍历原来出库单商品信息，记录不在新信息中的商品，视为删除
        List<OblistDetail> deleteList = new ArrayList<>();
        for (OblistDetail oblistDetail : oblistDetails) {
            boolean flag = false;
            for (OblistDetail o : goodsList) {
                if(oblistDetail.getId()==o.getId()){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                deleteList.add(oblistDetail);
            }
        }
        //遍历新信息,有ID则更新，无则插入
        for(OblistDetail goods : goodsList){
            if(goods.getId()!=null){
                oblistDetailMapper.updateOblistDetail(goods);
            }else{
                oblistDetailMapper.insertOblistDetail(goods);
            }
        }
        //删除不在新信息中的商品
        for(OblistDetail o : deleteList){
            oblistDetailMapper.deleteOblistDetailById(o.getId());
        }
        return AjaxResult.success();
    }


    /**
     * 审核出库单
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult reviewList(Long id) {
        //查询出库单信息
        OutboundList outboundList = outboundListMapper.selectOutboundListById(id);
        //判断是否已审核
        if (!outboundList.getStatus().equals("0")) {
            return AjaxResult.error("订单状态错误，无法审核");
        }
        //查询出库单明细信息
        OblistDetail oblistDetail_select = new OblistDetail();
        oblistDetail_select.setListid(id);
        List<OblistDetail> oblistDetails = oblistDetailMapper.selectOblistDetailList(oblistDetail_select);
        //遍历出库单明细信息，更新库存,插入变更记录
        for (OblistDetail oblistDetail : oblistDetails) {
            //更新库存
            StockLog stockLog = new StockLog();
            stockLog.setListid(id);
            stockLog.setCompanyId(outboundList.getCompanyId());
            stockLog.setCompanyName(outboundList.getCompanyName());
            stockLog.setShopName(outboundList.getShopName());
            stockLog.setShopId(outboundList.getShopId());
            stockLog.setGoodsid(oblistDetail.getGoodsid());
            stockLog.setGoodsName(oblistDetail.getGoodsName());
            stockLog.setRemark(oblistDetail.getRemark());
            stockLog.setInOut("2");
            stockLog.setGoodsNum(-oblistDetail.getGoodsNum());
            stockLog.setType(outboundList.getType());
            int res = stockLogServiceImpl.insertStockLog(stockLog);
            if(res<0){
                throw new RuntimeException("库存更新失败");
            }
        }
        //更新出库单状态
        outboundList.setStatus("5");
        outboundList.setReviewTime(IOUtil.getdate("yyyy-MM-dd HH:mm:ss"));
        outboundListMapper.updateOutboundList(outboundList);
        return AjaxResult.success();
    }

    /**
     * 查询出库单
     * 
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public OutboundList selectOutboundListById(Long id)
    {
        return outboundListMapper.selectOutboundListById(id);
    }

    /**
     * 查询出库单列表
     * 
     * @param outboundList 出库单
     * @return 出库单
     */
    @Override
    public List<OutboundList> selectOutboundListList(OutboundList outboundList)
    {
        return outboundListMapper.selectOutboundListList(outboundList);
    }

    /**
     * 新增出库单
     * 
     * @param data 出库单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOutboundList(JSONObject data)
    {
        //出库单信息
        OutboundList outboundList = JSON.parseObject(data.toJSONString(), OutboundList.class);
        outboundList.setDay(IOUtil.getdate("yyyy-MM-dd"));
        //插入出库单信息
        int result = outboundListMapper.insertOutboundList(outboundList);
        //出库单明细信息
        List<OblistDetail> outboundLists = JSON.parseArray(data.getJSONArray("goods").toJSONString(), OblistDetail.class);

        //批量插入出库单明细信息
        if (result > 0) {
            for (OblistDetail oblistDetail : outboundLists) {
                oblistDetail.setListid(outboundList.getId());
            }
            result = oblistDetailMapper.insertMulity(outboundLists);
        }
        return result;
    }

    /**
     * 修改出库单
     * 
     * @param outboundList 出库单
     * @return 结果
     */
    @Override
    public int updateOutboundList(OutboundList outboundList)
    {
        return outboundListMapper.updateOutboundList(outboundList);
    }

    /**
     * 批量删除出库单
     * 
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Override
    public int deleteOutboundListByIds(Long[] ids)
    {
        return outboundListMapper.deleteOutboundListByIds(ids);
    }

    /**
     * 删除出库单信息
     * 
     * @param id 出库单主键
     * @return 结果
     */
    @Override
    public int deleteOutboundListById(Long id)
    {
        return outboundListMapper.deleteOutboundListById(id);
    }
}
